<?php

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/zz.php';
if (file_exists($cron_lang))
{
    global $_LANG;

    include_once($cron_lang);
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'zz_add_desc';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = '';

    /* 版本号 */
    $modules[$i]['version'] = '1.0.0';

    /* 配置信息 */
    $modules[$i]['config']  = '';

    return;
}

$where =  "WHERE oi.order_status=".OS_CONFIRMED;
$where .= " AND oi.pay_status=".PS_PAYED;
$where .= " AND oi.zz_status=".ZZ_NOTPUSH;
$where .= " AND oi.cus_status=".CIQ_PUSHED;
$where .= " AND oi.ciq_status=".CIQ_PUSHED;
$where .= " AND oi.shipping_status=".SS_UNSHIPPED;
$where .= " AND og.suppliers_id=2"; //卓志仓

// $limit = " LIMIT ".$num;

$orderBy = " ORDER BY oi.add_time DESC ";

$sql = "SELECT oi.*,og.goods_number,og.goods_price,og.goods_sn,og.goods_name,g.tax_price FROM ".
		$GLOBALS['ecs']->table('order_info')." AS oi ".
		" LEFT JOIN ".$GLOBALS['ecs']->table('order_goods')." AS og ON og.order_id=oi.order_id ".
		" LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=og.goods_id ".
		$where.$orderBy.$limit;

$goods = $GLOBALS['db']->getAll($sql);
// print_r($goods);
// echo count($goods);
if (empty($goods)) exit;

require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/zhuozhi.php');
$temp = array();
foreach ($goods as $key=>$row)
{
	$temp[$row['order_id']]['orderId'] = $row['pp_sn'];
	$temp[$row['order_id']]['orderDate'] = local_date("Y-m-d H:i:s",$row['add_time']);
	$temp[$row['order_id']]['customerId'] = '1000000202';
	$temp[$row['order_id']]['packingMaterial'] = null;
	$temp[$row['order_id']]['warehouseId'] = null;
	$temp[$row['order_id']]['tpl'] = '0000025';
	$temp[$row['order_id']]['orderType'] = 2;
	$temp[$row['order_id']]['electriccode'] = '1000000202';
	$temp[$row['order_id']]['cbepcomcode'] = '1000000202';
	$temp[$row['order_id']]['deliveryCode'] = null;
	$temp[$row['order_id']]['notes'] = null;
	
	$temp[$row['order_id']]['recipient']['name'] = $row['consignee'];
	$temp[$row['order_id']]['recipient']['receiveType'] = 1;
	$temp[$row['order_id']]['recipient']['receiveNo'] = $row['credit_card'];
	$temp[$row['order_id']]['recipient']['mobilePhone'] = $row['mobile'];
	$temp[$row['order_id']]['recipient']['phone'] = $row['tel'];
	$temp[$row['order_id']]['recipient']['country'] = qin_get_region($row['country']);
	$temp[$row['order_id']]['recipient']['province'] = qin_get_region($row['province']);
	$temp[$row['order_id']]['recipient']['city'] = qin_get_region($row['city']).'市';
	$temp[$row['order_id']]['recipient']['district'] = qin_get_region($row['district']);
	$temp[$row['order_id']]['recipient']['address'] = qin_get_region($row['province']).' '.qin_get_region($row['city']).'市'.' '.qin_get_region($row['district']).' '.$row['address'];
	$temp[$row['order_id']]['recipient']['postCode'] = $row['zipcode'];
	

// 	$good['goodId'] = $row['goods_sn'];
	$good['goodId'] = '4901301230829';
	$good['amount'] = intval($row['goods_number']);
	$price = $row['goods_price']-$row['tax_price'];
	$good['price'] = sprintf("%.2f",$price);
	

	$temp[$row['order_id']]['goodList'][] = $good;
}
// var_dump($temp);
$total = count($temp);
$snArr = array();
// $result = array();
foreach ($temp as $k=>$v)
{
// 	print_r($v);
	$result = add_order($v);
	print_r($result);
// 	$order_sn = get_order_sn_by_pp($v['orderId']);
// 	push_log($order_sn, $result['status'], $result['notes'], '订单推送卓志','','',2);
	
// 	$zz_status = ($result['status'] == 1)?ZZ_PUSHED:ZZ_PUSHFAIL;
// 	$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET zz_status='$zz_status' WHERE pp_sn='$v[orderId]' ";
	
	
// 	$GLOBALS['db']->query($sql);
}






?>